package com.suyang.syt.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.ehcache.search.SearchException;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.suyang.syt.annotation.CurrentUser;
import com.suyang.syt.model.User;
import com.suyang.syt.model.dto.SystemNavigationDTO;
import com.suyang.syt.service.interfaces.SystemNavigationService;

/**
 * Created by Administrator on 2016/1/22.
 */
@Controller
public class IndexController {
	@Autowired
	private SystemNavigationService systemNavigationService;

	@RequestMapping("/")
	public String index(@CurrentUser User loginUser, HttpServletRequest request, Model model) {
		System.out.println("中转");
		System.out.println(loginUser.getIsAdmin());
		if (loginUser.getIsAdmin()) {
			System.out.println("============");
			request.getSession().setAttribute("loginUser", loginUser);
			return "redirect:/admin/index";
		}
		return "redirect:/front/index";
	}

	@RequestMapping("/admin/index")
	public String adminIndex(@CurrentUser User loginUser, Model model) throws SearchException {
		System.out.println("后台");
		try {
			Subject subject = SecurityUtils.getSubject();
			System.out.println(subject.hasRole("ceshi"));
			List<SystemNavigationDTO> menus = systemNavigationService.list("listNavAdmin");
			model.addAttribute("menus", menus);
			model.addAttribute("user", loginUser);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "index";
	}

	@RequestMapping("/front/index")
	public String front(@CurrentUser User loginUser, Model model) {
		System.out.println("前台");
		return "front/index";
	}
}
